package com.example.lib_apply

import androidx.lifecycle.viewModelScope
import com.example.lib_common.mvi.BaseViewModel
import com.example.lib_common.mvi.ResponseEntity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch

class ApplyViewModel :BaseViewModel<ApplyRepository,ApplyState>() {
    override fun createRepo(): ApplyRepository {
        return  ApplyRepository()
    }

    override fun createState(): ApplyState {
        var list :List<ApplyEntity> =ArrayList()
        return ApplyState(ResponseEntity(0,"", list))
    }

    fun requestAll(map: HashMap<String,Any>){
        mutableContainer.updateRequestState { copy(
            request = true
        ) }
        viewModelScope.launch(Dispatchers.IO) {
            repo.requestAllApply(map).flowOn(Dispatchers.IO)
                .collect{
                    mutableContainer.updateRequestState { copy(
                        request=false,complete=true
                    ) }
                    mutableContainer.updateUiState { copy(
                        entity=it
                    ) }
                }
        }
    }
}